package com.myproject.myapp.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;

import com.myproject.myapp.dao.MariaDao;
import com.myproject.myapp.dao.impl.MariaDaoImpl;
import com.myproject.myapp.service.MariaService;
import com.myproject.myapp.service.impl.MariaServiceImpl;


@Configuration
public class RootConfig {

	@Value("${jdbc.driverClassName}")
	private String driverClassName;
	
	@Value("${jdbc.url}")
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Value("${jdbc.password}")
	private String password;
	
	
	private static final String APP_CONFIG_FILE_PATH = "application.xml";
	
	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
		
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setLocations(new Resource[] { new ClassPathResource(APP_CONFIG_FILE_PATH)});
		return ppc;
	}
	
	@Bean
	public BasicDataSource dataSource(){
		
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		return dataSource;
	}
	
	@Bean
	public JdbcTemplate jdbcTemplate(){
		
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(this.dataSource());
		return jdbcTemplate;
	}
	
	@Bean
	public MariaService mariaService(){
		return new MariaServiceImpl();
	}
	
	@Bean
	public MariaDao mariaDao(){
		return new MariaDaoImpl();
	}
	
}
